clear all 
set maxvar 10000
set more off

* set up the working directory in your computer
cd 


* ------------------  Wave 1 : person data file ------------------ *
use klowf_w1_p,clear

* household/person ID 
gen pid_1 = p100id01 
gen hid_1 = h100hhid 

* dataset information including survey weights 
gen wave = 1
gen np = 0
gen wt = p1wei

* =========== basic demographic characteristics =============== *
* ----- respondents'
gen r_married = p199ml01 ==1 if ~missing(p199ml01)
gen r_marital = pp107ml02 if pp107ml02 >=0
gen r_cohab = p199lt01  == 1 if ~missing(p199lt01)
gen r_sex = p199sx 
gen r_age = p199ag 
gen r_educ = pp199ed15 if pp199ed15 >=0
gen r_work = p199jh01 == 1 if ~missing(p199jh01)

* past job history 
gen r_pastjob = w109jh02 ==1 if w109jh02 > 0 
recode w109sj01 (min/0=.)
recode w109tw09d (min/0=.)

gen r_pastjob_d = w109tw09d + w109tw09e/12 - w109sj01 - w109sj03/12 
replace r_pastjob_d = 0 if r_pastjob == 0

* ----- partners'
gen p_age = pp199ag_f if pp199ag_f >= 0
gen p_educ = pp199ed15_f if pp199ed15_f >= 0
gen p_work = pp199jh01_f ==1 if ~missing(pp199jh01_f)


* =========== major dependent/independent variables =============== *
* respondent income 
gen r_income = 0
replace r_income = r_income + ww115in02 if ~missing(ww115in02) & ww115in02 >= 0 
replace r_income = r_income + w116in02 if ~missing(w116in02) & w116in02 >= 0 
replace r_income = r_income + w116in03a /12 if ~missing(w116in03a) & w116in03a >0 
replace r_income = r_income + w117in02b if ~missing(w117in02b) & w117in02b >= 0 
replace r_income = r_income + w108in03b if ~missing(w108in03b) & w108in03b >= 0 
replace r_income = . if r_work == 0 

egen r_income_missing = anycount(ww115in02 w116in02 w116in03a w117in02b w108in03b), values(-8 -9)
replace r_income = . if r_income_missing > 0

* partner income 
gen p_income = .
replace p_income = pp109in02_k if ~missing(pp109in02_k) & pp109in02_k >=0  
replace p_income = p109in02_l  if ~missing(p109in02_l) & p109in02_l >= 0  
replace p_income = p109in02_n  if ~missing(p109in02_n) & p109in02_n >= 0 
replace p_income = 0 if p109rj == 3 
replace p_income = 0 if p109rj == 4 

replace p_work = 0 if p109jh03 == 2
replace p_income = . if p_work == 0

gen p_income_missing = missing(p_income) 

* ----- birth parity
gen b_experience = p115pg01 == 1 if ~missing(p115pg01)
gen b_number = p115cn01 if ~missing(p115cn01)
replace b_number = 0 if b_experience == 0 

* ----- network embeddedness measures 
recode p107ml11_1 (-9=.) (1=8) (2=4) (3=2) (4=1) (5=0), gen(n_dyad_movie)
recode p107ml11_2 (-9=.) (1=8) (2=4) (3=2) (4=1) (5=0), gen(n_dyad_activity)

recode p107ml11_3 (-9=.) (1=8) (2=4) (3=2) (4=1) (5=0) (6=0), gen(n_triad_visit_hpar)
recode p107ml11_4 (-9=.) (1=8) (2=4) (3=2) (4=1) (5=0) (6=0), gen(n_triad_visit_wpar)

recode p122lt03 (-9=.) (1=1) (2/5=0), gen(n_livetogether_wpar)
recode p123lt04 (-9=.) (-8=.) (1=1) (2/5=0), gen(n_livetogether_hpar)

replace n_triad_visit_wpar = 0 if p122ap == 4
replace n_triad_visit_hpar = 0 if p123ap == 4
replace n_livetogether_wpar = 0 if p122ap == 4
replace n_livetogether_hpar = 0 if p123ap == 4


keep wave np pid_* hid_* r_* p_* b_* n_* wt
save klowf_analysis_1p, replace 

* -------------------- Wave 2 : Existing Panel ---------------------------- *
use klowf_w2_p,clear

* person/household ID
gen pid_1 = p100id01 
gen hid_1 = h100hhid 
gen pid_2 = p200id02 
gen hid_2 = h200hhid 

* dataset information including survey weights 
gen wave = 2
gen np = 0
gen wt = p2wei_l

* ------ birth related 
gen b_experience = p215pg01 == 1 if ~missing(p215pg01) & p215pg01 >=0 
gen b_number = p215pg02 if ~missing(p215pg02) 
replace b_number = 0 if b_experience == 0 


keep wave np wt pid_* hid_* b_* 
save klowf_analysis_2p, replace 

* ------------------- Wave 3 : Existing Panel ---------------------------- *
use klowf_w3_p,clear

* person/household ID 
gen pid_1 = p100id01 
gen pid_2 = p200id02 
gen pid_3 = p300id03 
gen hid_1 = h100hhid 
gen hid_2 = h200hhid 
gen hid_3 = h300hhid 

* dataset information including survey weights 
gen wave = 3
gen np= 0
gen wt = p3wei_l

* ----- birth related 
gen b_experience = p315pg01 == 1 if ~missing(p315pg01) & p315pg01 >=0 
gen b_number = p315pg02 if ~missing(p315pg02) 
replace b_number = 0 if b_experience == 0 

keep wave np pid_* hid_* b_* wt
save klowf_analysis_3p, replace 


* ---------------- Wave 1 : Household Level Data ------------------------- *
use klowf_w1_h,clear

* household ID 
gen hid_1 = h100hhid 

* household income 
gen h_income = hh103ti03 if hh103ti03 >= 0

* household assets
gen h_as_selfhouse = h102hs02 if h102hs02 >= 0
gen h_as_borrowhouse = h102hs03 if h102hs03 >= 0
gen h_as_borrowhouse_temp = h102hs04 if h102hs04 >= 0
gen h_as_boo = h105re02 if h105re02 >= 0 
gen h_as_boo_temp = h105re04 if h105re04 >= 0 
gen h_as_tangible = hh105ta11 if hh105ta11 >= 0
gen h_as_finance = hh105fa14 if hh105fa14 >= 0

egen h_asset = rowtotal(h_as_*)

gen wave = 1
keep wave hid_* h_* 
save klowf_analysis_1h, replace 

* ------------------- merge data sets 
use klowf_analysis_1p, clear
merge m:1 hid_1 using klowf_analysis_1h, gen(m_h1)
append using klowf_analysis_2p
append using klowf_analysis_3p

order pid_1 pid_2 pid_3  hid_1 hid_2 hid_3 wave

gen id = .
replace id = pid_1 if missing(id)
replace id = pid_2 if missing(id)
replace id = pid_3 if missing(id)

gen hid = . 
replace hid = hid_1 if missing(hid)
replace hid = hid_2 if missing(hid)
replace hid = hid_3 if missing(hid)

xtset id wave 

save klowf_analysis, replace 


